local jLootz = jLootz
local lib = jLootz:GetModule("Frame")

local function Button_OnEnter(self)
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
	GameTooltip:SetLootItem(self:GetParent().slot)
end

local function Button_OnLeave(self)
	if GameTooltip:IsOwned(self) then
		GameTooltip:Hide()
	end
end

local function Button_OnClick(self)
	local slot = self:GetParent().slot
	if not slot then return end
	lib:SetMasterTarget(self, slot)
	if IsModifiedClick() then
		HandleModifiedClick(GetLootSlotLink(slot))
	else
		LootSlot(slot)
	end
end

local function Frame_OnHide(self)
	self.slot = nil
	self.quality:Hide()
	self.desc:Hide()
	self.bind:Hide()
end

local function make_button_texture(button, texture, size, offsety)
	local t = button:CreateTexture()
	t:SetTexture(texture)
	t:SetWidth(size)
	t:SetHeight(size)
	t:SetPoint("CENTER", 0, offsety or 0)
	return t
end

function lib:MakeLootFrame()
	local frame = CreateFrame("Frame", nil, self.frame)
	frame:SetScript("OnHide", Frame_OnHide)

	local button = CreateFrame("Button", nil, frame)
	button:SetScript("OnEnter", Button_OnEnter)
	button:SetScript("OnLeave", Button_OnLeave)
	button:SetScript("OnClick", Button_OnClick)
	button:SetWidth(37)
	button:SetHeight(37)

	local icon = button:CreateTexture(nil, "BORDER")
	icon:SetAllPoints()
	frame.icon = icon
	
	local count = button:CreateFontString(nil, "BORDER")
	count:SetFontObject("NumberFontNormal")
	count:SetJustifyH("RIGHT")
	frame.count = count
	count:SetPoint("BOTTOMRIGHT", button, -2, 1)
	
	button:SetNormalTexture(make_button_texture(button, "Interface\\Buttons\\UI-Quickslot2", 64, -1))
	button:SetPushedTexture(make_button_texture(button, "Interface\\Buttons\\UI-Quickslot-Depress", 36))
	
	local t = make_button_texture(button, "Interface\\Buttons\\ButtonHilight-Square", 36)
	t:SetBlendMode("ADD")
	button:SetHighlightTexture(t)

	button:SetPoint("TOPLEFT")
	frame.button = button
	button:SetPoint("LEFT")
	
	local name = frame:CreateFontString(nil, "ARTWORK")
	name:SetFontObject("ChatFontSmall")

	name:SetPoint("TOPLEFT", button, "TOPRIGHT", 2, 0) 
	frame.name = name
	name:SetPoint("TOPLEFT", button, "TOPRIGHT", 2, -2)
	
	local quality = frame:CreateFontString(nil, "ARTWORK")
	quality:SetFontObject("ChatFontSmall")

	quality:SetPoint("TOPRIGHT")
	frame.quality = quality
	quality:SetPoint("TOPRIGHT", -2, -4)
	
	local desc = frame:CreateFontString(nil, "ARTWORK")
	desc:SetFontObject("ChatFontSmall")

	desc:SetPoint("BOTTOMLEFT", button, "BOTTOMRIGHT")
	frame.desc = desc
	desc:SetPoint("BOTTOMLEFT", button, "BOTTOMRIGHT", 2, 2)
	
	local bind = frame:CreateFontString(nil, "ARTWORK")
	bind:SetFontObject("ChatFontSmall")

	bind:SetPoint("BOTTOMRIGHT")
	frame.bind = bind
	bind:SetPoint("BOTTOMRIGHT", -2, 2)
	
	local buttons = self.buttons
	local index = #buttons
	if index == 0 then
		frame:SetPoint("TOPLEFT", self.frame, 8, -8)
	else
		frame:SetPoint("TOPLEFT", buttons[index], "BOTTOMLEFT")
	end
	buttons[index + 1] = frame
	frame:SetWidth(200)
	frame:SetHeight(40)
	
	return frame
end

function lib:FillLootFrame(frame, slot, texture, item, quantity, quality, locked)
	if not texture then
		frame:Hide()
		return
	end
	frame.slot = slot
	frame.icon:SetTexture(texture)
	frame.name:SetTextColor(GetItemQualityColor(quality))
	if quantity > 1 then
		frame.count:SetText(quantity)
		frame.count:Show()
	else
		frame.count:Hide()
	end
	if LootSlotIsCoin(slot) then
		frame.name:SetText(item:gsub("\n", ", "))
		frame.quality:Hide()
		frame.desc:Hide()
		frame.bind:Hide()
	else
		frame.name:SetText(item)

		frame.quality:SetText(_G["ITEM_QUALITY"..quality.."_DESC"])
		frame.quality:Show()
		frame.quality:SetTextColor(GetItemQualityColor(quality))

		local link = GetLootSlotLink(slot)
		if link then
			local type, subtype, _, loc = select(6, GetItemInfo(link))
			loc = (loc and loc ~= "" and _G[loc] .. ", ") or ""
			if subtype ~= type then
				type = type .. " " .. subtype
			end
			frame.desc:SetText(loc..type)
			frame.desc:Show()
			frame.desc:SetTextColor(GetItemQualityColor(quality))
		else
			frame.desc:Hide()
		end
		--[[ @FIXME frame.bind:Show() ]]
	end	
end

function lib:GetLootFrame()
	local buttons = self.buttons
	local n = (buttons.count or 0) + 1
	local b = buttons[n]
	if not b then
		b = self:MakeLootFrame()
	end
	buttons.count = n
	b:Show()
	return b
end

function lib:ClearLootFrames()
	local buttons = self.buttons
	for i = 1, (buttons.count or 0) do
		buttons[i]:Hide()
	end
	buttons.count = nil
end
